//
//  FacePanelView.m
//  HCWeibo
//
//  Created by huiwenjiaoyu on 15/12/21.
//  Copyright © 2015年 www.iphonetrain.com 无限互联. All rights reserved.
//

#import "FacePanelView.h"
#import "FaceCollectionViewCell.h"

#define itemHight 45
#define itemWidth kScreenWidth / 7
#define faceHight 30
#define faceWidth 30

@implementation FacePanelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubviews];
    }
    return self;
}

- (NSArray *)faces
{
    if (_faces == nil) {
        _faces = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"emoticons.plist" ofType:nil]];
    }
    return _faces;
}

- (void)createSubviews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHight);
    flowLayout.minimumInteritemSpacing = 0.0;
    flowLayout.minimumLineSpacing = 0.0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 5 * itemHight) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.pagingEnabled = YES;
    _collectionView.backgroundColor = [UIColor lightGrayColor];
    [_collectionView registerClass:[FaceCollectionViewCell class] forCellWithReuseIdentifier:@"faceCell"];
    
    [self addSubview:_collectionView];
    
    CGSize size = [_pageControl sizeForNumberOfPages:4];
    CGFloat x = self.center.x - size.width * 0.5;
    CGFloat y = CGRectGetMaxY(_collectionView.frame) + 10;
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(x, y, size.width, size.height)];
    _pageControl.numberOfPages = 4;
    [self addSubview:_pageControl];
    
}

#pragma  mark - CollectionDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.faces.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FaceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"faceCell" forIndexPath:indexPath];
    UIImage *image = [UIImage imageNamed:[self.faces[indexPath.item] objectForKey:@"png"]];
    cell.image = image;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth;
}

@end
